package dados;
import negocios.cinemaBeans.*;
import negocios.excecoes.FRIException;
import negocios.excecoes.RepFilmeException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

public class RepositorioIngresso implements Serializable {
	private static RepositorioIngresso instanciaIngresso;
	private double valorIngresso;
	
	private RepositorioIngresso(){
		
	}
	
	public static RepositorioIngresso getInstanciaIngresso(){
		instanciaIngresso = recuperaArquivoIngresso();
		
		if(instanciaIngresso == null){
			instanciaIngresso = new RepositorioIngresso();
		}
		return instanciaIngresso;
	}
	
	public double getValorIngresso() {
		return valorIngresso;
	}

	public void setValorIngresso(double valor) {
		this.valorIngresso = valor;
		gravaArquivoIngresso();
	}
	
	public static void gravaArquivoIngresso(){
		File arquivoIngresso;
		FileOutputStream fos;
		ObjectOutputStream oos;
		
		try {
			arquivoIngresso = new File("ingresso.dat");
			fos = new FileOutputStream(arquivoIngresso);
			oos = new ObjectOutputStream(fos);
			oos.writeObject(instanciaIngresso);
			oos.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

		
	protected static RepositorioIngresso recuperaArquivoIngresso(){
		File arquivoIngresso;
		FileInputStream fis;
		ObjectInputStream ois;
		RepositorioIngresso clone = null;
		
		try{
			arquivoIngresso = new File("ingresso.dat");
			fis = new FileInputStream(arquivoIngresso);
			ois = new ObjectInputStream(fis);
			clone = (RepositorioIngresso)ois.readObject();
			ois.close();
		} catch (FileNotFoundException e) {
			return clone;
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return clone;		
	}

}
